package com.kinghood.productcenter.controller.base;

import com.kinghood.infrastructure.api.authority.SysLogModuleEnum;
import com.kinghood.infrastructure.aspect.OperatorLog;
import com.kinghood.infrastructure.model.KingHoodException;
import com.kinghood.infrastructure.model.KingHoodResponseVO;
import com.kinghood.infrastructure.model.dto.StatusReqDTO;
import com.kinghood.productcenter.common.model.dto.base.CodeRuleItemReqDTO;
import com.kinghood.productcenter.common.model.dto.base.CodeRuleReqDTO;
import com.kinghood.productcenter.common.model.dto.base.ListCodeRuleQueryDTO;
import com.kinghood.productcenter.service.base.IBaseCodeRuleService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 编码规则表 前端控制器
 * </p>
 *
 * @author KingHood
 * @since 2023-12-20
 */
@RestController
@RequestMapping("/baseCodeRule")
public class BaseCodeRuleController {
    @Resource
    private IBaseCodeRuleService codeRuleService;

    /**
     * 列表
     *
     * @author jp
     * @date 2023/12/20
     */
    @PostMapping("/listCodeRule")
    public KingHoodResponseVO listCodeRule(@RequestBody ListCodeRuleQueryDTO param) {
        return KingHoodResponseVO.success(codeRuleService.listCodeRule(param));
    }

    /**
     * 新增更新
     *
     * @param param
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_CODE_RULE, content = "编解规则管理-新增、修改规则")
    @PostMapping(value = "/addOrUpdateCodeRule")
    public KingHoodResponseVO addOrUpdateCodeRule(@Valid @RequestBody CodeRuleReqDTO param) {
        if (CollectionUtils.isEmpty(param.getItemList())) {
            throw new KingHoodException(-1, "规则值不能为空");
        }
        for (CodeRuleItemReqDTO itemReqDTO : param.getItemList()) {
            if (itemReqDTO.getRuleType() == 1 && Objects.isNull(itemReqDTO.getNumberType())) {
                throw new KingHoodException(-1, "规则值类型不能为空");
            }
        }
        codeRuleService.addorUpdateCodeRule(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 根据id删除
     *
     * @param id id
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_CODE_RULE, content = "编解规则管理-删除规则")
    @RequestMapping(value = "/delCodeRule/{id}")
    public KingHoodResponseVO delCodeRule(@PathVariable("id") Long id) {
        codeRuleService.delCodeRuleById(id);
        return KingHoodResponseVO.success();
    }

    /**
     * 根据ID查询详情
     *
     * @param id id
     * @author jp
     * @date 2023/12/21
     */
    @GetMapping(value = "/detailCodeRule/{id}")
    public KingHoodResponseVO detailCodeRule(@PathVariable("id") Long id) {
        return KingHoodResponseVO.success(codeRuleService.detailCodeRule(id));
    }

    /**
     * 更新状态
     *
     * @param param 参数
     * @author jp
     * @date 2023/12/21
     */
    @OperatorLog(module = SysLogModuleEnum.Type.PRODUCT_CODE_RULE, content = "编解规则管理-修改状态")
    @PostMapping(value = "/updateCodeRuleStatus")
    public KingHoodResponseVO updateCodeRuleStatus(@Valid @RequestBody StatusReqDTO param) {
        codeRuleService.updateCodeRuleStatus(param);
        return KingHoodResponseVO.success();
    }

    /**
     * 获取编码
     *
     * @return @return {@link KingHoodResponseVO }
     * @author jp
     * @date 2024/01/18 16:23
     */
    @GetMapping(value = "/getCodeByRule")
    public KingHoodResponseVO getCodeByRule(@RequestParam("code") String code) {
        return KingHoodResponseVO.success(codeRuleService.getCodeByRule(code));
    }
}
